﻿using Ebusiness_Domain.DomainService;
using Ebusiness_Domain.IRepository;
using Ebusiness_EntityFrameWork;
using Ebusiness_EntityFrameWork.DBContext;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.EntityFrameworkCore.MySQL;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.EntityFrameworkCore.SqlServer;
using AbpVnext_Module.DBContext;
using Volo.Abp.Uow;
using Microsoft.Extensions.Options;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.Auditing;
using Volo.Abp.AuditLogging;
using Volo.Abp.Identity;
using Volo.Abp.PermissionManagement;
using Volo.Abp.TenantManagement;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.OpenIddict.EntityFrameworkCore;

namespace AbpVnext__Comon
{
    /// <summary>
    /// 所有的项目，抽象成AbpModule
    /// 方便使用和维护。
    ///  项目服务隔离 ，好处：独立维护
    /// </summary>
    [DependsOn(typeof(DomainMoudule),
               //工作单元 事务
               //typeof(AbpUnitOfWorkModule),
               typeof(AbpEntityFrameworkCoreModule),
               typeof(AbpEntityFrameworkCoreMySQLModule),
               typeof(AbpEntityFrameworkCoreSqlServerModule)

                , typeof(AbpPermissionManagementEntityFrameworkCoreModule) //权限管理
               , typeof(AbpIdentityEntityFrameworkCoreModule) //身份认证
                                                              //, typeof(AbpAuditLoggingEntityFrameworkCoreModule) //审计日志
        , typeof(AbpSettingManagementEntityFrameworkCoreModule)  //设置模块
         , typeof(AbpTenantManagementEntityFrameworkCoreModule) //多租户
        , typeof(AbpFeatureManagementEntityFrameworkCoreModule) // 特征模块
        , typeof(AbpOpenIddictEntityFrameworkCoreModule) //分布式认证模块
        )]
    public class EntityFrameWorkMoudule : AbpModule
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="context"></param>
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            #region abp efcore 数据库上下文原理
            {
                //  ProductABPController
                //     ->ProductABPService
                //         ->ProductABPRepository
                //              > EfCoreRepository
                //                   核心Volo.Abp.EntityFrameworkCore项目
                //                     -> lDbContextProvider ->实现类UnitOfWorkDbContextProvider
                //                         Volo.Abp.MultiTenancy项目
                //                              -> IConnectionStringResolver -> 实现类MultiTenantConnectionStringResolver
                //                                  -> ITenantStore-- > 实现类TenantStore
                //                                        Volo.Abp.TenantManagement顶目
                //                                           ->ITenantRepository--->实现类EfCoreTenantRepository
                //                                                 ->查询数据库
            }
            #endregion

            //base.ConfigureServices(context); 依赖注入 代码注册
            //context.Services.AddTransient<IUserService, UserService>();
            //1、配置EfCoreContext
            //context.Services.AddDbContext<EbusinessDbContext>(options =>
            //{
            //    // 2、配置MySQL
            //    options.UseMySql("server=192.168.211.129;port=3306;database=AspNetCore_ProductImage_Test;uid=root;pwd=123456",
            //         //指定程序集目录
            //         new MySqlServerVersion(new Version(5, 7, 29)), b => b.MigrationsAssembly("Ebusiness_Host"));
            //});
            //注册默认全局上下文
            //dotnet ef migrations add project1  -c EbusinessDbContext   --project  "D:\学习\项目\Abp_DDD\abp\webApi\Ebusiness_EntityFrameWork\Ebusiness_EntityFrameWork.csproj"
            context.Services.AddDbContext<EbusinessDbContext>(options =>
            {
                // 2、配置MySQL
                options.UseMySql("server=127.0.0.1;port=3306;database=AspNetCore_ProductImage;uid=root;pwd=123456",
                     //指定程序集目录
                     new MySqlServerVersion(new Version(5, 7, 29)));
            });

            //生成迁移文件
            //dotnet ef  migrations add project1   -c  ProjectDbContext   --project  "D:\学习\项目\Abp_DDD\abp\webApi\Ebusiness_EntityFrameWork\Ebusiness_EntityFrameWork.csproj"
            //dotnet ef  database update   -c  ProjectDbContext   --project  "D:\学习\项目\ABP\abp\webApi\Ebusiness_EntityFrameWork\Ebusiness_EntityFrameWork.csproj"
            //注册ABP全局上下文 ProjectDbContext
            context.Services.AddAbpDbContext<ProjectDbContext>();

            //dotnet ef  migrations add project1   -c  ProductImageDbContext   --project  "D:\学习\项目\Abp_DDD\abp\webApi\Ebusiness_EntityFrameWork\Ebusiness_EntityFrameWork.csproj"
            //注册商品图片上下文 注册ABP全局上下文
            context.Services.AddAbpDbContext<ProductImageDbContext>();

            //context.Services.AddTransient<IProductRepository, ProductRepository>();

            ///注册身份认证的服务依赖  
            ///泛型俩个参数分别是用户数据模型和角色模型
            ///AddEntityFrameworkStores 通过这个函数连接数据库上下文对象   泛型为对应的上下文对象。
            //微软Identity数据库上下文
            //context.Services.AddDbContext<AppDbContext>(options =>
            //       options.UseMySql("server=127.0.0.1;port=3306;database=AspNetCore_ProductImage_Identity;uid=root;pwd=123456",
            //            //指定程序集目录
            //            new MySqlServerVersion(new Version(5, 7, 29)))
            //   );
            //集成mysql   
            Configure<AbpDbContextOptions>(options =>
            {
                options.UseMySQL();
                //var config = configuration.GetSection("DatabaseType").Value;
                //Console.WriteLine(config);
                ////动态切换数据
                //if (config.Equals("SqlServer"))
                //{
                //    option.UseSqlServer();
                //}
                //else
                //{
                //    option.UseMySQL();
                //}
            });

            context.Services.AddTransient<ProductMannage>();
        }
    }
}
